package com.info3k.client.components;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.domain.User;
import com.info3k.client.enums.TabTypes;
import com.info3k.client.tabs.Tab;

public class TabManager {
	private List<Tab> tabs = new ArrayList<Tab>();
	
	HorizontalPanel tabTop = new HorizontalPanel();
	VerticalPanel tabLeft = new VerticalPanel();
	
	private Info3K info3k;
	
	public TabManager(VerticalPanel headerRight, Info3K info){
		this.info3k = info;
		headerRight.add(tabTop);
		RootPanel.get("info3k_left_section").add(tabLeft);
		tabLeft.setWidth("100%");
	}
	
	public void addTab(TabTypes type) {
		if(type.toString().contains("_LEFT")){
			Tab tab = Tab.getTabByType(type);
			tab.setInfo3k(info3k);
			addLeftTab(tab);
		} else if(type.toString().contains("_TOP")){
			Tab tab = Tab.getTabByType(type);
			tab.setInfo3k(info3k);
			addTopTab(tab);
		}
	}
	
	public void switchToStandardTabs(){
		removeTabs();
    	addTab(TabTypes.LATEST_ADDRESSES_TOP);
    	addTab(TabTypes.LATEST_LOCATIONS_TOP);
    	addTab(TabTypes.LATEST_EVENTS_TOP);
    	addTab(TabTypes.CATEGORIES_LEFT);
    	addTab(TabTypes.LATEST_USERS_LEFT);
	}
	
	public void switchToUserTabs(User u){
    	removeTabs();
    	for (TabTypes type : u.getTabs()) {
			addTab(type);
		}
	}
	
	public void addTopTab(Tab tab){
		tab.setWidth("170px");
		tabTop.add(tab);
		tabs.add(tab);
	}
	
	public void addLeftTab(Tab tab){
		tabLeft.add(new HTML("<div class=\"h_line\"></div>"));
		tabLeft.add(tab);
		tabs.add(tab);
	}
	
	public void removeTabs(){
		tabLeft.clear();
		tabTop.clear();
	}
	
	public void refresh(TabTypes type){
		for (Tab tab : tabs) {
			if(tab.getType().equals(type)){
				tab.refresh();
			}
		}
	}
	
	public Integer tabCount(){
		return tabs.size();
	}
}